package com.cjs.example.provider.service;

import com.cjs.example.provider.api.WorkflowService;
import com.cjs.example.provider.util.SecurityUtil;
import org.activiti.api.process.model.ProcessInstance;
import org.activiti.api.process.model.builders.ProcessPayloadBuilder;
import org.activiti.api.process.model.payloads.GetProcessInstancesPayload;
import org.activiti.api.process.runtime.ProcessRuntime;
import org.activiti.api.runtime.shared.query.Order;
import org.activiti.api.runtime.shared.query.Page;
import org.activiti.api.runtime.shared.query.Pageable;
import org.activiti.api.task.model.Task;
import org.activiti.api.task.model.builders.TaskPayloadBuilder;
import org.activiti.api.task.runtime.TaskRuntime;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.repository.Deployment;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Map;

/**
 * @Author: ChengJianSheng
 * @Date: 2022/12/6
 */
@DubboService
public class WorkflowServiceImpl implements WorkflowService {

    @Autowired
    private ProcessRuntime processRuntime;

    @Autowired
    private TaskRuntime taskRuntime;

    @Autowired
    private RuntimeService runtimeService;

    @Autowired
    private RepositoryService repositoryService;

    @Autowired
    private SecurityUtil securityUtil;


    @Override
    public void deploy() {
        Deployment deployment = repositoryService.createDeployment()
                .addClasspathResource("processes/leave.bpmn20.xml")
                .name("请假")
                .key("leave")
                .category("AAA")
                .tenantId("QingJia")
                .deploy();
    }

    @Override
    public void start() {
        securityUtil.logInAs("tom");
        ProcessInstance processInstance = processRuntime.start(ProcessPayloadBuilder.start()
                .withProcessDefinitionKey("leave")
                .withVariable("hello", "world")
                .withVariable("apple", "orange")
                .withBusinessKey("1")
                .build());
    }

    @Override
    public void startProcessInstance(String processDefinitionKey, String businessKey, Map<String, Object> variables, String tenantId) {
        runtimeService.startProcessInstanceByKeyAndTenantId(processDefinitionKey, businessKey, variables, tenantId);
    }

    @Override
    public void processInstancePage(int pageNo, int pageSize) {
        securityUtil.logInAs("tom");
        GetProcessInstancesPayload payload = new GetProcessInstancesPayload();
        payload.setActiveOnly(true);
        Page<ProcessInstance> page = processRuntime.processInstances(Pageable.of(pageNo-1, pageSize, Order.by("start_time_", Order.Direction.DESC)), payload);
        for (ProcessInstance ps : page.getContent()) {
            System.out.println(ps);
        }
    }

    @Override
    public void taskList() {
        securityUtil.logInAs("zhangsan");
        Page<Task> taskPage = taskRuntime.tasks(Pageable.of(0, 10));
        System.out.println(taskPage.getTotalItems());

        securityUtil.logInAs("lisi");
        taskPage = taskRuntime.tasks(Pageable.of(0, 10));
        System.out.println(taskPage.getTotalItems());
    }

    @Override
    public void claimTask() {
        securityUtil.logInAs("zhangsan");
        String taskId = "429ad159-754c-11ed-aaf8-84a9386654d8";
        Task task = taskRuntime.task(taskId);
        taskRuntime.claim(TaskPayloadBuilder.claim().withTaskId(taskId).build());
    }

    @Override
    public void completeTask() {
        securityUtil.logInAs("zhangsan");
        String taskId = "429ad159-754c-11ed-aaf8-84a9386654d8";
        taskRuntime.complete(TaskPayloadBuilder.complete().withTaskId(taskId).build());
    }
}
